import QtGraphicalEffects 1.15
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
import QtQuick.Controls.Material 2.15
import QtQuick.VirtualKeyboard 2.4
import QtQuick.VirtualKeyboard.Settings 2.4
import "qrc:/style"
import "qrc:/store"
import "qrc:/window"
import "qrc:/components"
import QtQuick.Layouts 1.15


Window {
    id: mainWindow
    visible: true
    width: 800
    height: 600
    title: qsTr("水站管理系统 - 测试版")

    Material.theme: Material.Light
    Material.accent: Material.Blue
    Material.primary: Material.White

    // flags: Qt.FramelessWindowHint | Qt.Window
    // visibility: Window.FullScreen
    property real modBaseUnit: Math.min(width, height) / 100
    property real baseUnit: modBaseUnit * 1.33
    // 添加全局阴影控制
    property bool enableShadows: false  // 默认关闭所有阴影
    // 添加滑动相关属性
    property real swipeThreshold: width * 0.1  // 滑动阈值
    property real startX: 0                    // 开始滑动的X坐标
    property point lastClickPos: Qt.point(0, 0)
    Rectangle {
        anchors.fill: parent
        color: "#90d7ec"  // 使用单一颜色
    }
    CustomMessageBox {
        id: messageBox
    }
    CustomFinish {
        id: finish
    }
    Component.onCompleted: {
        var com = Qt.platform.os === "linux" ? "/dev/ttyUSB0" : "COM4"
        if(!modbusHelper.createModbus_rtu(com, 38400,31)){
            console.log("modbusHelper连接失败:" + modbusHelper.errorString)
        }else{
            console.log("modbusHelper连接成功")
        }

        // modSlave.createRTU("COM4", 9600,17)
    }
    ModMain {
        id: modMain
        anchors.fill: parent
        stackView.transform: Translate {
            id: contentTransform
            y: 0
            
            Behavior on y {
                NumberAnimation { 
                    duration: 200 
                    easing.type: Easing.OutQuad
                }
            }
        }
        
    }

    Snakebar {
        id: snackbar
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
    }


    Connections {
        target: modSlave
        function onErrorOccurred(str) {
            messageBox.show(str+": Will Exit Slave.", "error")
        }
    }
    
    Connections {
        target: moduleHelper
        function onFinishOperation(str) {
            messageBox.show(str, "success")
        }
    }
    Connections {
        target: moduleHelper
        function onShowFinish(type) {
            finish.show(type)
        }
    }
    Connections {
        target: moduleHelper
        function onSysError(str) {
            messageBox.show(str, "error", 0)
            // snackbar.show(str, "error", 0)
        }
    }
    Connections {
        target: moduleHelper
        function onCommError() {
            // messageBox.show("通讯错误", "error",1000)
            snackbar.showError("通讯错误")
        }
    }
    InputPanel {
        id: inputPanel
        z: 99
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        visible: active

        onActiveChanged: {
            if (active) {
                if(lastClickPos.y > inputPanel.y){
                    contentTransform.y = inputPanel.height - lastClickPos.y - baseUnit * 10
                }
            } else {
                contentTransform.y = 0
            }
        }
    }
    MouseArea {
        anchors.fill: parent
        preventStealing: true  // 防止事件被偷取
        onPressed: {
            lastClickPos = Qt.point(mouseX, mouseY)
            mouse.accepted = false
        }
        onClicked: mouse.accepted = false
    }

    // 更新检查器
    UpdateDialog {
        id: updateDialog
        anchors.centerIn: parent
        
    }

    Connections {
        target: updateChecker
        function onUpdateAvailable(version) {
            updateDialog.currentVersion = updateChecker.currentVersion
            updateDialog.latestVersion = updateChecker.latestVersion
            updateDialog.releaseNotes = updateChecker.releaseNotes
            updateDialog.forceUpdate = updateChecker.forceUpdate
            updateDialog.open()
        }
        
        function onUpdateError(error) {
            errorDialog.text = error
            errorDialog.open()
        }
        
        function onIsDownloadingChanged(isDownloading) {
            updateDialog.isDownloading = isDownloading
        }
        function onDownloadProgressChanged(downloadProgress) {
            updateDialog.downloadProgress = downloadProgress
        }
    }

    // 更新对话框
   

    // 错误对话框
    ErrorDialog {
        id: errorDialog
        anchors.centerIn: parent
    }

    function showUpdateDialog(version, url) {
        updateDialog.version = version
        updateDialog.url = url
        updateDialog.open()
    }

    function showErrorDialog(error) {
        errorDialog.text = error
        errorDialog.open()
    }

    function updateProgress(progress) {
        updateDialog.updateProgress(progress)
    }

}

